package com.lagou.task1.work4;

import java.util.*;

public class StudentOper {

    private Map<Integer,Student> dataMap = new HashMap<>();

    private boolean addStudent(Student student){
        if (dataMap.get(student.getNum())!=null){
            System.out.println("学号为" + student.getNum() + "的学生已存在");
            return false;
        }
        dataMap.put(student.getNum(),student);
        System.out.println("添加学生成功");
        return true;
    }

    private boolean delStudent(int num){
        if (dataMap.get(num)==null){
            System.out.println("学号为" + num + "的学生不存在");
            return false;
        }
        dataMap.remove(num);
        System.out.println("删除学生成功");
        return true;
    }

    private boolean updateStudent(int num,String name,int age){
        if (dataMap.get(num)==null){
            System.out.println("学号为" + num + "的学生不存在");
            return false;
        }
        Student student = dataMap.get(num);
        student.setName(name);
        student.setAge(age);
        dataMap.put(num,student);
        System.out.println("修改学生成功"+student);
        return true;
    }

    private boolean findStudent(int num){
        if (dataMap.get(num)==null){
            System.out.println("学号为" + num + "的学生不存在");
            return false;
        }
        System.out.println("获取学生信息成功：" + dataMap.get(num));
        return true;
    }

    private void showStudent(){
        Set<Integer> integers = dataMap.keySet();
        for (Integer integer : integers) {
            Student student = dataMap.get(integer);
            System.out.println(student);
        }
    }

    public void operStuMsg(Scanner scanner){
        System.out.println("------------------------");
        System.out.println("请根据序号选择以下功能");
        System.out.println("1.添加学生信息");
        System.out.println("2.删除学生信息");
        System.out.println("3.修改学生信息");
        System.out.println("4.查找学生信息");
        System.out.println("5.查看所有学生信息");
        System.out.println("请输入序号");
        String str = scanner.next();
        switch (str){
            case "1":
                System.out.println("请输入学生学号、姓名、年龄(以空格进行分割)");
                int num = scanner.nextInt();
                String name = scanner.next();
                int age = scanner.nextInt();
                Student student = new Student(num, name, age);
                addStudent(student);
                break;
            case "2":
                System.out.println("请输入学生学号进行删除");
                num = scanner.nextInt();
                delStudent(num);
                break;
            case "3":
                System.out.println("请输入学生学号、姓名、年龄进行修改(以空格进行分割)");
                num = scanner.nextInt();
                name = scanner.next();
                age = scanner.nextInt();
                updateStudent(num,name,age);
                break;
            case "4":
                System.out.println("请输入学生学号进行查询");
                num = scanner.nextInt();
                findStudent(num);
                break;
            case "5":
                showStudent();
                break;
            default:
                System.out.println("无此选项请重新输入！");
                break;
        }
        this.operStuMsg(scanner);
    }

    private List<Student> dataList = new ArrayList<>();

    public void operStuMsg1(Scanner scanner){
        System.out.println("------------------------");
        System.out.println("请根据序号选择以下功能");
        System.out.println("1.添加学生信息");
        System.out.println("2.删除学生信息");
        System.out.println("3.修改学生信息");
        System.out.println("4.查找学生信息");
        System.out.println("5.查看所有学生信息");
        System.out.println("请输入序号");
        int str = scanner.nextInt();
        switch (str){
            case 1:
                System.out.println("请输入学生学号、姓名、年龄(以空格进行分割)");
                int num = scanner.nextInt();
                String name = scanner.next();
                int age = scanner.nextInt();
                Student student = new Student(num, name, age);
                StudentAdd.addStudent(student,dataList);
                break;
            case 2:
                System.out.println("请输入学生学号进行删除");
                num = scanner.nextInt();
                StudentDel.delStudent(num,dataList);
                break;
            case 3:
                System.out.println("请输入学生学号、姓名、年龄进行修改(以空格进行分割)");
                num = scanner.nextInt();
                name = scanner.next();
                age = scanner.nextInt();
                StudentUpdate.updateStudent(num,name,age,dataList);
                break;
            case 4:
                System.out.println("请输入学生学号进行查询");
                num = scanner.nextInt();
                StudentFind.findStudent(num,dataList);
                break;
            case 5:
                System.out.println("所有学生的信息为：");
                StudentFind.showStudent(dataList);
                break;
            default:
                System.out.println("无此选项请重新输入！");
                break;
        }
        this.operStuMsg1(scanner);
    }

}
